﻿

namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// NotifyWindow.xaml 的交互逻辑
/// </summary>
[VampirewalIoCRegister("NotifyWindow", RegisterType.Transient)]
public partial class NotifyWindow : WindowBase, INotifyWindow
{
    public NotifyWindow()
    {
        InitializeComponent();

        this.Loaded += NotifyWindow_Loaded;
    }

    public void Setting(NotifyWindowSetting setting)
    {
        NotifySetting = setting;
        SetValue(setting);
    }

    private NotifyWindowSetting NotifySetting { get; set; }

    private void NotifyWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var animation = new DoubleAnimation
        {
            Duration = new Duration(TimeSpan.FromSeconds(0.5)),
            To = SystemParameters.WorkArea.Bottom - this.Height,
        };
        this.BeginAnimation(TopProperty, animation);

        Left = SystemParameters.WorkArea.Right - this.Width - 10;
        Top = SystemParameters.WorkArea.Bottom;
    }

    private void SetValue(NotifyWindowSetting Setting)
    {
        Width = Setting.WindowWidth;
        Height = Setting.WindowHeight;

        if (Setting.UcView != null)
        {
            UcView.Content = Setting.UcView;

            Setting.UcView.Width = double.NaN;
            Setting.UcView.Height = double.NaN;

            var vm = Setting.UcView.DataContext as ViewModelBase;

            if (vm != null)
            {
                vm.PassData(Setting.PassData);
            }
        }
        else
        {
            NotifyDefultView notifyDefultView = new NotifyDefultView();
            UcView.Content = notifyDefultView;
        }

        if (!string.IsNullOrEmpty(Setting.NotifyTitle))
        {
            this.Title = Setting.NotifyTitle;
        }

        if (!string.IsNullOrEmpty(Setting.Background))
        {
            this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.Background));
        }
        else
        {
            this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2F3437"));
        }

        if (!string.IsNullOrEmpty(Setting.Foreground))
        {
            this.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.Foreground));
        }
        else
        {
            this.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EBEBEB"));
        }

        if (!string.IsNullOrEmpty(Setting.WindowBorderBrush))
        {
            this.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.WindowBorderBrush));
        }
        else
        {
            this.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3F4447"));
        }

        this.TitleFontSize = Setting.TitleFontSize;

        if (Setting.DelayCloseTime > 0)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, Setting.DelayCloseTime);
            timer.Tick += (s, e) =>
            {
                while (Setting.DelayCloseTime > 0)
                {
                    Setting.DelayCloseTime--;
                }

                if (Setting.DelayCloseTime == 0)
                {
                    var animation = new DoubleAnimation
                    {
                        Duration = new Duration(TimeSpan.FromSeconds(0.3)),
                        To = SystemParameters.WorkArea.Bottom,
                    };
                    animation.Completed += (ss, ee) =>
                    {
                        this.Close();
                    };
                    this.BeginAnimation(TopProperty, animation);
                }
            };

            timer.IsEnabled = true;
            timer.Start();
        }
    }

    protected override void TopTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        var animation = new DoubleAnimation
        {
            Duration = new Duration(TimeSpan.FromSeconds(0.3)),
            To = SystemParameters.WorkArea.Bottom,
        };
        animation.Completed += (ss, ee) =>
        {
            this.Close();
        };
        this.BeginAnimation(TopProperty, animation);
    }
}

///// <summary>
///// Notify窗体配置
///// </summary>
//public class NotifyWindowSetting
//{
//    /// <summary>
//    /// Notify窗体标题
//    /// </summary>
//    public string NotifyTitle { get; set; } = "系统通知";

//    /// <summary>
//    /// 显示窗体
//    /// </summary>
//    public FrameworkElement UiView { get; set; }

//    /// <summary>
//    /// 窗体宽度
//    /// </summary>
//    public double WindowWidth { get; set; } = 300d;

//    /// <summary>
//    /// 窗体高度
//    /// </summary>
//    public double WindowHeight { get; set; } = 450d;

//    /// <summary>
//    /// 窗体背景色
//    /// </summary>
//    public string Background { get; set; } = "#2F3437";

//    /// <summary>
//    /// 窗体前景色
//    /// </summary>
//    public string Foreground { get; set; } = "#EBEBEB";

//    /// <summary>
//    /// 标题文字大小
//    /// </summary>
//    public int TitleFontSize { get; set; } = 12;

//    /// <summary>
//    /// 窗体边框颜色
//    /// </summary>
//    public string WindowBorderBrush { get; set; } = "#3F4447";

//    private int _DelayCloseTime = 0;

//    /// <summary>
//    /// 延迟关闭时间(默认以秒计算)
//    /// <para>默认等于0，大于0即认为开启延迟关闭</para>
//    /// </summary>
//    public int DelayCloseTime
//    {
//        get
//        {
//            if (_DelayCloseTime < 0)
//            {
//                _DelayCloseTime = 0;
//            }

//            return _DelayCloseTime;
//        }
//        set
//        {
//            _DelayCloseTime = value;
//        }
//    }

//    /// <summary>
//    /// 需要传输的数据
//    /// </summary>
//    public object PassData { get; set; }
//}